package com.cdivtc.shiro;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;


public class demo {
    public static void main(String[] args) throws Exception {

        Class c2 = Student.class;


//创建实例
        Object person = c2.newInstance();


/*

        Method[] methods = c2.getDeclaredMethods();
        for (Method method : methods) {

            Field field = c2.getDeclaredField(method.getName().toString());
            field.set(person,"asdfasdfasdf");
            System.out.println((Student) person);//work waid equls toString hashCode等

        }
*/


        //1.获取Class对象
        Class stuClass = Student.class;
        //2.获取字段
        System.out.println("************获取所有公有的字段********************");
        Field[] fieldArray = stuClass.getFields();
        for (Field f : fieldArray) {
            System.out.println(f);
        }
        System.out.println("************获取所有的字段(包括私有、受保护、默认的)********************");
        fieldArray = stuClass.getDeclaredFields();
        for (Field f : fieldArray) {
            System.out.println(f);
        }
        System.out.println("*************获取公有字段**并调用***********************************");


        Student p = new Student();
        Field nameField = p.getClass().getDeclaredField("name");

// 由于name成员变量是private, 所以需要进行访问权限设定
        nameField.setAccessible(true);

// 使用反射进行赋值
        nameField.set(p, "sdfsdfsdfsd");

// 打印查看被修改后的值
        System.out.println("   p.getName() = " + p.getName());

        List list = new ArrayList();
        Field[] declaredFields = p.getClass().getDeclaredFields();
        for (Field field : declaredFields
        ) {
            list.add(field.getName());
        }


    }
}
